package com.shawn.server.controller;

import com.shawn.commons.utils.CommentType;
import com.shawn.commons.utils.RC;
import com.shawn.model.vo.CommentsVo;
import com.shawn.model.vo.PageResult;
import com.shawn.server.interceptor.ThreadContext;
import com.shawn.server.service.CommentsService;
import com.shawn.server.service.FreezeService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 评论控制器
 *
 * @author lenovo
 * @date 2022/10/29
 */
@RestController
@RequestMapping("/comments")
public class CommentsController {

    @Autowired
    private CommentsService commentsService;

    @Autowired
    private FreezeService freezeService;

    /**
     * 得到所有评论
     *
     * @param movementId 动态id
     * @param page       页面
     * @param pageSize   页面大小
     * @return {@link ResponseEntity}
     */
    @GetMapping
    public ResponseEntity getAllComments(String movementId, @RequestParam(defaultValue = "1") Integer page,@RequestParam(name = "pagesize",defaultValue = "10") Integer pageSize){
            PageResult<CommentsVo> pageResult = commentsService.getAllComments(movementId,page,pageSize);
            return ResponseEntity.ok(pageResult);
    }

    /**
     * 对动态发表评论
     *
     * @param map 地图
     * @return {@link ResponseEntity}
     */
    @PostMapping
    public ResponseEntity publishComments(@RequestBody Map<String,String> map){
        freezeService.checkFreeze(ThreadContext.getUserId(), RC.FREEZING_COMMENT);
        //获取信息
        String movementId = map.get("movementId");
        String comment = map.get("comment");
        commentsService.publishComments(movementId, CommentType.COMMENT.getType(), comment);
        return ResponseEntity.ok("评论已发表");
    }

    /**
     * 根据评论id对评论进行点赞
     *
     * @return {@link ResponseEntity}
     */
    @GetMapping("/{id}/like")
    public ResponseEntity commentLike(@PathVariable String id){
        Long userId = ThreadContext.getUserId();
       Integer likeCount = commentsService.commentWhetherLike(userId,id,CommentType.COMMENT_LIKE.getType());
        return ResponseEntity.ok(likeCount);
    }
    /**
     * 根据评论id对评论进行取消点赞
     *
     * @return {@link ResponseEntity}
     */
    @GetMapping("/{id}/dislike")
    public ResponseEntity commentDisLike(@PathVariable String id){
        Long userId = ThreadContext.getUserId();
        Integer likeCount = commentsService.commentWhetherLike(userId,id,CommentType.DISLIKE.getType());
        return ResponseEntity.ok(likeCount);
    }



}
